package com.ramblingcoder.bc2.server.tool.update.commands;

import java.io.IOException;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;


public class Login {
	public static boolean plainText(Socket socket, String password) throws IOException{
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"login.plainText", password});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK")){
			return true;
		} else {
			return false;
		}
	}

	public static boolean hashed(Socket socket, String password) throws IOException, NoSuchAlgorithmException {
		
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"procon.login.username", "member"});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equalsIgnoreCase("ok") == false){
			return false;
		}
		
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"login.hashed"});
		response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equalsIgnoreCase("ok") == false){
			return false;
		}
		
		String hash = response.getWordsTrimmed()[0];
		
		String hashedPassword = SupportingFunctions.hashPassword(hash, password);
		
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"login.hashed", hashedPassword});
		response = SupportingFunctions.getResponse(socket.getInputStream())[0];

		if (response.getStatus().equals("OK")){
			return true;
		} else {
			return false;
		}
	}
}
